<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../jquery.min.js"></script>
<script type="text/javascript" src="../jquery.easyui.min.js"></script>
<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>

<script src="../js/xheditor/xheditor-1.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/addTab.js"></script>
<!-- <script type="text/javascript" src="../js/jquery.cookie.js"></script> -->
</head>
<body>
	<div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'west',split:true" title="West" style="width:200px;">
				<ul id="tt"></ul>				
				<a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-add" onclick="addNews()" plain="true">添加新闻</a> 
				<input type="text" name="parentId" id="parentId" style="display:none"/>
        </div>

		
    	
		
        <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">

        	<table id="dg" data-options="toolbar:'#search'"></table>

        	<!--搜索功能-->      	
        	<div class="wu-toolbar-search" id="search">        		           
	            <label>关键词：</label><input class="wu-text" style="width:100px">
	            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'"  iconCls="icon-search" id="titleSearch">开始检索</a>
	        </div>

        </div>
    </div>
    
    <div id="news-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
    	 <form id="news-form" class="easyui-form" method="post" data-options="novalidate:true">
    	 	<div style="margin-bottom:20px">
		        <input class="easyui-textbox" name="title" style="width:100%" data-options="label:'新闻标题:',required:true">
		    </div>
		    <div style="margin-bottom:20px">
		        <input class="easyui-textbox" name="author" style="width:100%" data-options="label:'作者:',required:true">
		    </div>	
		    <div style="margin-bottom:20px">
		        <input class="easyui-textbox" name="subhead" style="width:100%" data-options="label:'副标题:',required:true">
		    </div>
		    <div style="margin-bottom:20px">		    	
		       新闻内容：<textarea name="content" rows="30" cols="60" class="xheditor" data-options="required:true"></textarea>	
		    </div>
	 		 		
	 	</form> 
	</div>
    
    <script>
		var nodeId = null;
		
    	$('#tt').tree({
    		animate:true,
		    url:'http://localhost:3000/cate/list',
		    method:'get',
		    onClick: function(node){		    	
				initList(node._id);
				$("#parentId").val(node._id);
			}
		});

    	var title = null;
		function initList(cateId){
			//console.log(cateId);
			var postUrl;
			if(cateId !=undefined && cateId.trim().length>0){
				if(title){
					queryParams = {'title': title}
				}else{
					queryParams = {cateId: cateId}
				}				
			}else{
				queryParams = {};
			}			
			$("#dg").datagrid({
				url:'http://localhost:3000/news/list',
				method:'post',
				pagination:true,
				pageSize:2,
				queryParams: queryParams,
				pageList:[2,4,6,8,10],
				fit:true,
				columns:[[
			        {field:'title',title:'新闻标题',width:80},
			        {field:'author',title:'作者',width:80},
			        {field:'subhead',title:'副标题',width:80},
			        {field:'content',title:'新闻内容',width:150},
			        {field:'date',title:'发布时间',width:160},
			        {field:'cateId',title:'所属分类',width:180},
			        {field:'operate',title:'在线管理', width:150,
						formatter: function(value,row,index){							
							return '<a href="javascript:void(0)" onclick="editNews(\''+ row._id+'\')" class="editcls">编辑</a> '+
							'<a href="javascript:void(0)" onclick="deleNews(\''+ row._id+'\')" class="removecls">删除</a>'+
							"<a href='javascript:void(0)' onclick='addTab(\"评论管理\",\"comment/comment.html?id="+row._id+"\",\"icon-user-group\",1)'>评论</a>"
						}
					}
			    ]],
			    onLoadSuccess:function(data){  
	                $('.editcls').linkbutton({text:'修改',plain:true,iconCls:'icon-edit'}); 
	                $('.removecls').linkbutton({text:'删除',plain:true,iconCls:'icon-remove'});
	            }
			})		
		}
		
		initList();
		
		function editNews(id){//修改
			nodeId = id;
			var obj = $('#dg').datagrid('getSelected');
			$('#news-form').form('clear');
			$("#news-dialog").dialog({
				closed: false,
				modal:true,
		        title: "修改新闻内容",
		        onOpen:function(){
		        	$("#news-form").form('load',obj);
		        },
		        buttons: [{
		            text: '确定',
		            iconCls: 'icon-ok',
		            handler: newsEditAction
		        }, {
		            text: '取消',
		            iconCls: 'icon-cancel',
		            handler: function () {
		                $('#news-dialog').dialog('close');                    
		            }
		        }]
		    });
		}
		
		function newsEditAction(){
			$('#news-dialog').form('submit',{
		        onSubmit:function(){
		            if ($(this).form('enableValidation').form('validate')){
						$.ajax({
							type:"post",
							url:"http://localhost:3000/news/data/"+nodeId,
							data:$("#news-form").serializeJSON()
						}).then(function(res){							
							$('#dg').datagrid('reload');
						});
		            }else{
		            	alert('no');
		            }
		            $('#news-dialog').dialog('close');					
		        }
		    });
		}
		
		
		function deleNews(id){//删除
			$.ajax({
				type:"delete",
				url:"http://localhost:3000/news/data/"+ id
			}).then(function(res){		
				$('#dg').datagrid('reload');
			});
		}
		

		
		var cateId = null;
		function addNews(){//添加新闻
			cateId = $("#parentId").val();
			$('#news-form').form('clear');
			$("#news-dialog").dialog({
				closed: false,
				modal:true,
		        title: "添加新闻内容",
		        buttons: [{
		            text: '确定',
		            iconCls: 'icon-ok',
		            handler: newsAddAction
		        }, {
		            text: '取消',
		            iconCls: 'icon-cancel',
		            handler: function () {
		                $('#news-dialog').dialog('close');                    
		            }
		        }]
		    });
		}
		
		function newsAddAction(){
			var addata = $("#news-form").serializeJSON();
			if(cateId){
				addata.cateId = cateId;
			}else{
				alert('请选中类名');
			}
			
			$('#news-dialog').form('submit',{
		        onSubmit:function(){
		            if ($(this).form('enableValidation').form('validate')){
		            	//console.log(nodeId);
						$.ajax({
							type:"post",
							url:"http://localhost:3000/news/data",
							data:addata
						}).then(function(res){						
							$('#dg').datagrid('reload');
						});
		            }else{
		            	alert('no');
		            }
		            $('#news-dialog').dialog('close');					
		        }
		    });
		}
		
		//搜索		
		
		$('#titleSearch').on('click',function(){
			title = $('#search input').val();
			initList(title);
			title = null;
		});	
		
	</script>
</body>
</html>
